<template>
	<gui-page ref="guiPage" :customHeader="true">
		<template v-slot:gHeader>
			<nav-bar-custom left-icon="left" :title="$t('hardwareOut.pageTitle')"></nav-bar-custom>
		</template>
		<template v-slot:gBody>
			<view class="distributed">
				<view class="gui-bg-white gui-dark-bg-level-3 gui-padding-x distributed-head">
					<view class="gui-form-item gui-border-b">
						<text class="gui-form-label gui-secondary-text"
							style="width:200rpx">{{ $t('distributedEnter.odd') }}</text>
						<view class="gui-form-body gui-text-right">
							<input type="text" class="gui-form-input gui-text-right" v-model="fromData.instockCode"
								:placeholder="$t('distributedEnter.placeholderText')" />
						</view>
					</view>
					<view class="gui-form-item gui-border-b">
						<text class="gui-form-label gui-secondary-text" style="width:200rpx">{{
							$t('purchaseReturn.documentType')
							}}：</text>
						<view class="gui-form-body gui-text-right" style="overflow: visible;">
							<superweiCombox style="text-align: right;" :candidates="data.returnType"
								:placeholder="$t('distributedOut.placeholder')" :isJSON="true" keyName="name"
								@select="selectReturnType" v-model="fromData.returnType"
								:defaultValue="$t('purchaseReturn.materialVoucherType')">
							</superweiCombox>
						</view>
					</view>
					<view class="gui-form-item gui-border-b" v-if="fromData.returnTypeId!=='VO'">
						<text class="gui-form-label gui-secondary-text" style="width:200rpx">{{
							$t('smashRemoval.completionStatus')
							}}：</text>
						<view class="gui-form-body gui-text-right" style="overflow: visible;">
							<superweiCombox style="text-align: right;" :candidates="data.selectList"
								:placeholder="$t('distributedOut.placeholder')" :isJSON="true" keyName="name"
								@select="select" v-model="fromData.status" :defaultValue="$t('smashRemoval.all')">
							</superweiCombox>
						</view>
					</view>
					<view class="gui-form-item gui-border-b">
						<text class="gui-form-label gui-secondary-text">{{ $t('distributedEnter.date') }}</text>
						<view class="gui-form-body gui-text-right" @click="dateChoose">
							<view class="gui-form-input gui-text-right" v-if="fromData.chooseTime.length">
								<text>{{ fromData.chooseTime[0] }}</text> {{ fromData.chooseTime[1] ? '-' : '' }}
								<text>{{ fromData.chooseTime[1] }}</text>
							</view>
							<input v-else type="button" class="gui-form-input gui-text-right"
								:placeholder="$t('distributedEnter.placeholderTextChoose')" />
						</view>
					</view>
					<view class="gui-form-item gui-border-b gui-flex gui-justify-content-center">
						<view class="gui-flex gui-space-between gui-align-items-center"
							style="width: 70%;height: 100rpx;">
							<button type="default" class="gui-button gui-button-mini gui-bg-black4 gui-noborder"
								style="width:200rpx;" @click="reset">
								<text
									class="gui-block gui-color-black gui-button-text-mini gui-icons">{{ $t('distributedEnter.reset') }}</text>
							</button>
							<button type="default" class="gui-button gui-button-mini gui-bg-blue gui-noborder"
								style="width:200rpx;" @click="search">
								<text
									class="gui-block gui-color-white gui-button-text-mini gui-icons">{{ $t('distributedEnter.search') }}</text>
							</button>
						</view>
					</view>
				</view>
				<!-- 底部显示区域 -->
				<scroll-view :show-scrollbar="false" :scroll-y="true" class="scroll" @scrolltolower="scrolltolowerFn">
					<view style="height: 15rpx"></view>
					<view class="card-boxCustom gui-transition-all" v-for="(x, i) in data.list" @click="goPage(x)"
						:key="i">
						<view class="card-head">
							<text v-if="fromData.returnTypeId!=='VO' ">{{ $t('purchaseReturn.returnOrderNo') }}：{{ x.poCode }}</text>
							<text v-else>{{ $t('purchaseReturn.voucherNo') }}：{{ x.voCode }}</text>
						</view>
						<view class="card-body" style="padding-bottom: 15rpx;">
							<view class="card-rotating" @click.stop="data.list[i].openBool = !data.list[i].openBool">
								<uni-icons v-if="!data.list[i].openBool" type="bottom" color="#9C9C9C"
									size="20"></uni-icons>
								<uni-icons v-if="data.list[i].openBool" type="top" color="#9C9C9C"
									size="20"></uni-icons>
							</view>
							<view class="card-body-item">
								<text
									class="body-item-title">{{ $t('purchaseReturn.generateDate') }}：</text>{{ formatDate(x.returnDate) }}
							</view>
							<view class="card-body-item">
								<text class="body-item-title">{{ $t('hardware.receiptsQty') }}：</text> {{ x.returnTotalQty || 0 }}
							</view>
						</view>
						<view class="card-body-folding" v-show="data.list[i].openBool">
							<openView :keyName="'1'" :materialGroups="x.returnDetail"></openView>
						</view>
					</view>
					<view v-if="data.noticeHooks" class="noticeHooks">
						{{ data.list.length == 0 ? $t('stockManage.notAvailable') : $t('stockManage.without') }}
					</view>
				</scroll-view>
			</view>
			<calendarCustom ref="calendarCustomRef" @setTime="getTime" :selectTime="fromData.chooseTime">
			</calendarCustom>
		</template>
	</gui-page>
</template>

<script setup>
import searchCustom from '@/components/searchCustom';
import calendarCustom from '@/components/calendarCustom';
import openView from '@/components/common/openView';
import superweiCombox from '@/components/superweiCombox';
import { useStore } from "vuex";
import { basPdaOperationLogFn } from '@/utils/index.js';
import { OPERATION_SEARCH, OPERATION_REASCT, OPERATION_LOOK, OPERATION_CHOOSE } from '@/constant/index.js'
import { ref, reactive, onMounted, getCurrentInstance } from 'vue';
import { getWmsInStockPage } from '@/api/api.js'
// 引入 
import { onShow } from '@dcloudio/uni-app';
import graceJS from "@/Grace6/js/grace.js";
import { navigate } from '@/utils/routerSkip.js';
import { useI18n } from 'vue-i18n';

const { t } = useI18n();
let store = useStore();

let fromData = reactive({
	chooseTime: [],
	instockCode: '', // 入库单号
	status: '',//状态
	statusID: '',//状态
	returnType: 'VO',// 单据类型
	returnTypeId: 'VO',
	page: 1,
	rows: 10,
});
let data = reactive({
	list: [],
	noticeHooks: false, //暂无相关数据标识
	isFinish: false,
	isLoading: false,
	selectList: [{
		id: '',
		name: t('smashRemoval.all')
	}, {
		id: 'po_open',
		name: t('whether.closed')
	}, {
		id: 'po_close',
		name: t('whether.notClosed')
	}],
	returnType: [{
		id: 'VO',
		name: t('purchaseReturn.materialVoucherType')
	}, {
		id: 'PO',
		name: t('purchaseReturn.returnOrderType')
	}],
});
components: {
	searchCustom,
		calendarCustom,
		openView,
		superweiCombox
}
let calendarCustomRef = ref(null);
const { ctx } = getCurrentInstance();

onMounted(() => {

})
onShow( async() => {
	let time =await store.dispatch('getSystemTimeRequest') || ''
	let a=dataTime(time)
	fromData.chooseTime = [a,time]
	getRequestData(OPERATION_LOOK)
})

const goPage = (x) => {
	try {
		let inputParameter = x.instockCode
		let returnParameter = '无'
		let operationPicture = '五金仓采购退料'
		let callInterface = '无'
		let opertionParmes = {
			inputParameter,
			returnParameter,
			operationPicture,
			callInterface,
			responseCode: '无',//返回状态
			errorMessage: '无',//错误信息
			isPass: 'Y',//是否成功 Y N
			operationType: OPERATION_CHOOSE,
			module: 'WMS',
			source: "PDA",
		}
		basPdaOperationLogFn(opertionParmes)
	} catch (error) {

	}
	navigate({
		url: `/pages/hardware/hardwareOutDetail?poCode=${x.poCode}&voCode=${x.voCode}&returnTypeId=${fromData.returnTypeId}`
	})
}

const formatDate = (dateStr) => {
  if (!dateStr) return '';
  return dateStr.split(' ')[0];
};
// 进入页面默认日期00
const dataTime = (a) => {
	const end = (new Date(a).getTime() / 1000) - (60 * 60 * 24 * 7)
	const lastDate = new Date(parseInt(end) * 1000).getFullYear()
	const month = String(new Date(parseInt(end) * 1000).getMonth() + 1).padStart(2, '0'); // 补零
	const day = String(new Date(parseInt(end) * 1000).getDate()).padStart(2, '0'); // 补零
	return `${lastDate}-${month}-${day}`;
}

// 校验状态的选择事件
const select = (e) => {
	fromData.statusID = e.id;
	getRequestData(OPERATION_SEARCH)
}
// 校验状态的选择事件
const selectReturnType = (e) => {
	console.log(e, 'e')
	fromData.returnTypeId = e.id;
	getRequestData(OPERATION_SEARCH)
}

const search = () => {
	// 搜索的时候 重置请求页数
	fromData.page = 1
	getRequestData(OPERATION_SEARCH)
}
// 重置
const reset = async () => {
	// let time = await store.dispatch('getSystemTimeRequest') || ''
	// let a=dataTime(time)
	fromData.chooseTime = []
	fromData.page = 1
	fromData.instockCode = ''
	getRequestData(OPERATION_REASCT)
}
//上拉加载
const scrolltolower = () => {
	if (!data.noticeHooks) {
		fromData.page++
		getRequestData()
	}
}
const dateChoose = () => {
	calendarCustomRef.value.open();
}
const getTime = (times) => {
	fromData.chooseTime = times;
}
// 请求列表数据 
const getRequestData = async (type) => {
	let parmes = {
		isSp: 'Y',
		current: fromData.page,
		size: fromData.rows,
		instockTimeStart: fromData.chooseTime[0] || '', //入库开始时间
		instockTimeEnd: fromData.chooseTime[1] ? fromData.chooseTime[1] : (fromData.chooseTime[0] || ''), //入库结束时间
		poCode: fromData.instockCode, //入库单号
		status: fromData.statusID,
		returnType: fromData.returnTypeId //单据类型
	}
	getWmsInStockPage(parmes).then(res => {
		try {
			let inputParameter = JSON.stringify(parmes)
			let returnParameter = JSON.stringify(res)
			let operationPicture = '五金仓采购退料'
			let callInterface = res.rquestUrl
			let opertionParmes = {
				inputParameter,
				returnParameter,
				operationPicture,
				callInterface,
				responseCode: res.code,//返回状态
				errorMessage: res.msg,//错误信息
				isPass: res.code == 0 ? 'Y' : 'N',//是否成功 Y N
				operationType: type,
				module: 'WMS',
				source: "PDA",
			}
			basPdaOperationLogFn(opertionParmes)
		} catch (error) {
		}
		data.isLoading = false;
		if (res.code == 0) {
			data.noticeHooks = false
			// 根据接口返回设计上拉加载相应判断
			if (fromData.page === 1) {
				if (res.data.records.length == 0) {
					data.noticeHooks = true
				}
				data.list = res.data.records.reverse()
				data.list.forEach(element => {
					element.openBool = false
				});
			} else {
				let arr = res.data.records.reverse()
				arr.forEach(element => {
					element.openBool = false
				});
				data.list = [ ...data.list,...arr]
			}
			data.noticeHooks = data.list.length >= res.data.total
			data.isFinish = data.list.length >= res.data.total;
		}
	})
}

// 下拉加载
const scrolltolowerFn = () => {
	if (!data.isFinish && !data.isLoading) {
		fromData.page++
		data.isLoading = true;
		getRequestData()
	}
}
</script>

<style lang="scss" scoped>
.distributed-head {
	border-bottom-left-radius: 16rpx;
	border-bottom-right-radius: 16rpx;
}

.scroll {
	flex: 1;
	height: 2rpx;
}

.distributed {
	display: flex;
	flex-direction: column;
	height: calc(100vh - 88rpx);
	// height: 100vh;
}

.card-box {
	margin: 0 15rpx;
	box-shadow: 0rpx 0rpx 4rpx 0 #A5A5A5;
	background: #fff;
	padding: 30rpx;
	border-radius: 16rpx;
}

.card-item {
	line-height: 60rpx;
}

.card-bottom {
	margin-bottom: 30rpx;
}

.filtrate-item {
	display: flex;
	align-items: center;
	font-size: 32rpx;
	margin-bottom: 20rpx;

	.filtrate-itemLeft {
		height: 32rpx;
		line-height: 32rpx;
		width: 100rpx;
	}

	.filtrate-itemRight {
		border: 2rpx solid #A5A5A5;
		border-radius: 8rpx;
		overflow: hidden;
		line-height: 40rpx;
		width: 300rpx;
	}
}
</style>
